﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.ExceptionHandling;
using CSharp.Libs.Koc;
using CSharp.Libs.Misc;
using CSharp.Libs.Net;
using CSharp.Libs.WPF;
using System.Windows.Input;
using System.Collections;



namespace AllianceGetOtherInfo
{
    class MainWindowViewModel : ViewModelBase
    {
        #region declarations
        private ExceptionCollector _EC = null;
        private URLHolder _UH = null;

        private RelayCommand _rcAllianceGetOtherInfo = null;

        private String _strURLAllianceGetOtherInfo = "";
        private String _strResult = "";
        private Int32 _intPageCount = 1;

        //Constants
        private const String PARAM_PAGE_NO = "pageNo";

        private const String KEY_OTHER_ALLIANCES = "otherAlliances";
        private const String KEY_NAME = "name";
        private const String KEY_MIGHT = "might";
        private const String KEY_MEMBERS_COUNT = "membersCount";
        #endregion



        #region constructor / terminator
        public MainWindowViewModel()
        {
            _EC = new ExceptionCollector();
            _UH = new URLHolder(_EC);
        }

        ~MainWindowViewModel()
        {
            _UH = null;
            _EC.Dispose();
            _EC = null;
        }
        #endregion



        #region private functions
        private void _SetUrlAllianceGetOtherInfo()
        {
            try
            {
                _UH.ParseURLAllianceGetOtherInfo(_strURLAllianceGetOtherInfo);

                OnPropertyChanged("UrlAllianceGetOtherInfo");
                OnPropertyChanged("UrlAllianceGetOtherInfoValidness");
                OnPropertyChanged("PageCount");
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        private void _RefreshURL()
        {
            try
            {
                _strURLAllianceGetOtherInfo = _UH.URLAllianceGetOtherInfo.GetURL();
                OnPropertyChanged("UrlAllianceGetOtherInfo");
                OnPropertyChanged("UrlAllianceGetOtherInfoValidness");
                OnPropertyChanged("PageCount");
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        private void _ResetResult()
        {
            try
            {
                _strResult = "";
                OnPropertyChanged("Result");
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        private void _ExecuteAllianceGetOtherInfo()
        {
            try
            {
                if (_UH.URLAllianceGetOtherInfo.URLMaybeValid &&
                    _UH.URLAllianceGetOtherInfo.Parameters.ContainsKey(PARAM_PAGE_NO))
                {
                    //Prepare Result
                    _ResetResult();

                    //Create Header
                    String frmAlliList = "{0,-25}{1,20}{2,20}{3,20}";
                    _strResult = String.Format(frmAlliList, "Alli Name", "Macht", "Members", "Machtdurchschnitt") + Environment.NewLine;

                    for (Int32 PC = 1; PC <= _intPageCount; PC++)
                    {
                        //Set Parameters for URL
                        _UH.URLAllianceGetOtherInfo.Parameters[PARAM_PAGE_NO] = PC.ToString();
                        
                        //Fetch and show new URL
                        _RefreshURL();

                        //Prepare Answer-Hashtable
                        Hashtable Allis = null;
                        if (_UH.URLAllianceGetOtherInfo.GetAnswerHashtable(out Allis))
                        {
                            //Check if Answer is correct
                            if (Allis.ContainsKey(KEY_OTHER_ALLIANCES))
                            {
                                //Get Result-ArrayList
                                ArrayList AlliList = (ArrayList)Allis[KEY_OTHER_ALLIANCES];

                                //Cycle trough Allis
                                foreach (Hashtable htAlli in AlliList)
                                {
                                    Int32 Schnitt = Int32.Parse(htAlli[KEY_MIGHT].ToString()) / Int32.Parse(htAlli[KEY_MEMBERS_COUNT].ToString());
                                    _strResult = String.Concat(_strResult, String.Format(frmAlliList, htAlli[KEY_NAME], htAlli[KEY_MIGHT], htAlli[KEY_MEMBERS_COUNT], Schnitt.ToString()), Environment.NewLine);
                                }
                            }

                            OnPropertyChanged("Result");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion



        #region interface
        #region properties
        public String UrlAllianceGetOtherInfo
        {
            get
            {
                return _strURLAllianceGetOtherInfo;
            }
            set
            {
                _strURLAllianceGetOtherInfo = value;
                _SetUrlAllianceGetOtherInfo();
            }
        }

        public String UrlAllianceGetOtherInfoValidness
        {
            get
            {
                if (_UH.URLAllianceGetOtherInfo.URLMaybeValid)
                {
                    return "OK";
                }
                else
                {
                    return "FALSE";
                }
            }
        }

        public ICommand ExecuteAllianceGetOtherInfo
        {
            get
            {
                if (_rcAllianceGetOtherInfo == null)
                {
                    _rcAllianceGetOtherInfo = new RelayCommand(
                        param => this._ExecuteAllianceGetOtherInfo(),
                        param => this.CanExecuteAllianceGetOtherInfo);
                }
                return _rcAllianceGetOtherInfo;
            }
        }
        public Boolean CanExecuteAllianceGetOtherInfo
        {
            get
            {
                return true;
            }
        }

        public String Result
        {
            get
            {
                return _strResult;
            }
        }

        public Int32 PageCount
        {
            get
            {
                return _intPageCount;
            }
            set
            {
                if (value != _intPageCount &&
                    value > 0 &&
                    value < 9999)
                {
                    _intPageCount = value;
                }
                else
                {
                    _intPageCount = 1;
                }
            }
        }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
